package cn.iocoder.yudao.module.scenic.service.collect;

import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.module.scenic.controller.admin.checkin.vo.CheckInPageByLocationResVO;
import cn.iocoder.yudao.module.scenic.controller.app.collect.vo.SaveCollectPageReqVO;
import cn.iocoder.yudao.module.scenic.controller.app.collect.vo.SaveCollectPageResVO;
import cn.iocoder.yudao.module.scenic.controller.app.collect.vo.SaveCollectReq;
import cn.iocoder.yudao.module.scenic.dal.dataobject.collect.CollectDO;
import cn.iocoder.yudao.module.scenic.dal.dataobject.spot.SpotDO;
import cn.iocoder.yudao.module.scenic.dal.mysql.collect.CollectMapper;
import cn.iocoder.yudao.module.scenic.dal.mysql.spot.SpotMapper;
import cn.iocoder.yudao.module.scenic.service.spot.SpotService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import javax.cache.annotation.CacheResult;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @Description TODO
 * @Author jingming
 * @Date 2024/6/8 22:19
 */
@Service
@Validated
public class CollectServiceImpl implements CollectService {

    @Resource
    CollectMapper collectMapper;

    @Resource
    SpotMapper spotMapper;

    @Override
    public CommonResult<Long> save(SaveCollectReq pageReqVO) {
        QueryWrapper<CollectDO> wrapper = new QueryWrapper();
        wrapper.eq("spot_id",pageReqVO.getSpotId());
        wrapper.eq("open_id",pageReqVO.getOpenId());
        List<CollectDO> collectDOList = collectMapper.selectList(wrapper);
        if(CollectionUtil.isNotEmpty(collectDOList)){
            collectMapper.delete(wrapper);
            return CommonResult.success(collectDOList.get(0).getId());
        }else {
            CollectDO collectDO = new CollectDO();
            collectDO.setOpenId(pageReqVO.getOpenId());
            collectDO.setSpotId(pageReqVO.getSpotId());
            collectMapper.insert(collectDO);
            return CommonResult.success(collectDO.getId());
        }

    }

    @Override
    public PageResult<SaveCollectPageResVO> page(SaveCollectPageReqVO reqVO) {
        PageResult < SaveCollectPageResVO > pageResVOPageResult = new PageResult<>();
        IPage<CollectDO> allPage = new Page(reqVO.getPageNo(), reqVO.getPageSize());
        QueryWrapper<CollectDO> wrapper =new QueryWrapper();
        wrapper.eq("open_id",reqVO.getOpenId());
        wrapper.eq("deleted",false);
        wrapper.orderByDesc("create_time");
        List<CollectDO> collectDOList = collectMapper.selectList(allPage, wrapper);
        if(!CollectionUtil.isEmpty(collectDOList)) {
            List<Long> collect = collectDOList.stream().map(CollectDO::getSpotId).distinct().collect(Collectors.toList());
            QueryWrapper<SpotDO> spotDOQueryWrapper = new QueryWrapper();
            spotDOQueryWrapper.in("id", collect);
            List<SpotDO> spotDOS = spotMapper.selectList(spotDOQueryWrapper);
            Map<Long, SpotDO> collect1 = spotDOS.stream().collect(Collectors.toMap(SpotDO::getId, Function.identity()));
            List<SaveCollectPageResVO> collect2 = collectDOList.stream().map(s -> {
                SaveCollectPageResVO vo = new SaveCollectPageResVO();
                Long spotId = s.getSpotId();
                vo.setSpotId(spotId);
                if (collect1.containsKey(spotId)) {
                    SpotDO spotDO = collect1.get(spotId);
                    vo.setName(spotDO.getName());
                    vo.setPhotoUrl(spotDO.getPhotoUrl());
                }
                vo.setCreateTime(s.getCreateTime());
                return vo;
            }).collect(Collectors.toList());
            pageResVOPageResult.setList(collect2);
        }
        pageResVOPageResult.setTotal(allPage.getTotal());
        return pageResVOPageResult;
    }
}
